<?php

declare(strict_types=1);

namespace AdminBundle\Http\Controllers\Admin\V1;

use Hyperf\Di\Annotation\Inject;
use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use AdminBundle\Services\PermissionsService;
use App\Exception\BusinessException;

/**
 * Permission
 * 首页控制器
 * @package App\Controller\Admin
 */
class PermissionController extends AbstractController
{
    /**
     * @Inject
     * @var PermissionsService
     */
    public $service;

    /**
     * getUserPermissions
     * 获取当前用户拥有的权限
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getUserPermissions()
    {
        $userinfo = $this->request->getAttribute('auth');
        $list = $this->service->getUserPermissionsList($userinfo->id);

        $data = [
            'list' => $list
        ];

        return $this->response->success($data);
    }

    /**
     * index
     * 权限列表，权限管理
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function index()
    {
        $list = $this->service->getPermissionsTreeList();

        $data = [
            'list' => $list
        ];

        return $this->response->success($data);
    }

    /**
     * store
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     */
    public function store()
    {
        try {
            $reqParam = $this->request->all();
            if ( !(isset($reqParam['id']) && $reqParam['id']) ) {
                $reqParam['sort'] = $this->service->getPermissionsCount(['parent_id' => $reqParam['parent_id']]);
            }
    
            $info = $this->service->savePermissions($reqParam);
            return $this->response->success($info);
        } catch (\Throwable $throwable) {
            throw new BusinessException(ErrorCode::SERVER_ERROR, '操作失败');
        }
    }

    /**
     * getInfo
     * 根据id获取单条记录信息
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function getInfo()
    {
        $reqParam = $this->request->all();
        $info = $this->service->getInfo($reqParam['id']);

        $data = [
            'info' => $info,
        ];

        return $this->response->success($data);
    }

    /**
     * orderPermissions
     * 权限的拖拽排序
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     */
    public function orderPermissions()
    {
        $reqParam = $this->request->all();
        $this->service->orderPermissions($reqParam['ids']);

        return $this->response->success('ok');
    }

    /**
     * destroy
     * 删除权限
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     */
    public function destroy($id)
    {
        $count = $this->service->getPermissionsCount(['parent_id' => $id]);
        if ($count) {
            throw new BusinessException(ErrorCode::ERR_NO_DEL);
        }
        $this->service->deleteInfo($id);

        return $this->response->success('ok');
    }

}
